package com.fessc.develop.utils;

import lombok.extern.slf4j.Slf4j;

import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class LocalAddressUtil {

    public LocalAddressUtil() {
    }

    public static String getIpAddress() {
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;

            while(true) {
                NetworkInterface netInterface;
                do {
                    do {
                        do {
                            if (!allNetInterfaces.hasMoreElements()) {
                                return "";
                            }

                            netInterface = (NetworkInterface)allNetInterfaces.nextElement();
                        } while(netInterface.isLoopback());
                    } while(netInterface.isVirtual());
                } while(!netInterface.isUp());

                Enumeration addresses = netInterface.getInetAddresses();

                while(addresses.hasMoreElements()) {
                    ip = (InetAddress)addresses.nextElement();
                    if (ip != null && ip instanceof Inet4Address) {
                        return ip.getHostAddress();
                    }
                }
            }
        } catch (Exception e) {
            log.info("获取ip地址异常：" + e.getMessage());
            return "";
        }
    }

    public static String getMacAddress() {
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            Object var1 = null;

            while(true) {
                byte[] mac;
                do {
                    NetworkInterface netInterface;
                    do {
                        do {
                            do {
                                if (!allNetInterfaces.hasMoreElements()) {
                                    return "";
                                }

                                netInterface = (NetworkInterface)allNetInterfaces.nextElement();
                            } while(netInterface.isLoopback());
                        } while(netInterface.isVirtual());
                    } while(!netInterface.isUp());

                    mac = netInterface.getHardwareAddress();
                } while(mac == null);

                StringBuilder sb = new StringBuilder();

                for(int i = 0; i < mac.length; ++i) {
                    sb.append(String.format("%02X%s", mac[i], i < mac.length - 1 ? "-" : ""));
                }
            }
        } catch (Exception e) {
            log.info("获取mac地址异常：" + e.getMessage());
            return "";
        }
    }

    public static String getMac() {
        Enumeration<NetworkInterface> en = null;
        Object unique = new ArrayList();

        try {
            en = NetworkInterface.getNetworkInterfaces();
            StringBuilder str = new StringBuilder();
            ArrayList temp = new ArrayList();

            label80:
            while(en.hasMoreElements()) {
                NetworkInterface iface = (NetworkInterface)en.nextElement();
                List<InterfaceAddress> addrs = iface.getInterfaceAddresses();
                Iterator var6 = addrs.iterator();

                while(true) {
                    byte[] mac;
                    do {
                        NetworkInterface network;
                        do {
                            InetAddress ip;
                            do {
                                do {
                                    do {
                                        if (!var6.hasNext()) {
                                            continue label80;
                                        }

                                        InterfaceAddress addr = (InterfaceAddress)var6.next();
                                        ip = addr.getAddress();
                                    } while(iface.isLoopback());
                                } while(iface.isVirtual());
                            } while(!iface.isUp());

                            network = NetworkInterface.getByInetAddress(ip);
                        } while(network == null);

                        mac = network.getHardwareAddress();
                    } while(mac == null);

                    str.delete(0, str.length());

                    for(int i = 0; i < mac.length; ++i) {
                        str.append(String.format("%02X%s", mac[i], i < mac.length - 1 ? "-" : ""));
                    }

                    temp.add(str.toString());
                }
            }

            if (temp.size() <= 0) {
                return "00-00-00-00-00-00-00-00";
            }

            unique = (List)temp.stream().distinct().collect(Collectors.toList());
            Iterator var13 = ((List)unique).iterator();

            while(var13.hasNext()) {
                String s = (String)var13.next();
                log.info("MAC====" + s);
            }
        } catch (SocketException e) {
            log.info("获取ip地址异常：" + e.getMessage());
        }

        return (String)((List)unique).get(((List)unique).size() - 1);
    }

}
